package cn.com.headfree.mouldshow.dao.impl;

import cn.com.headfree.entity.TPersonYearPoint;
import cn.com.headfree.entity.TSalaryInfo;
import cn.com.headfree.entity.TYearEffectInfo;
import cn.com.headfree.entity.isc.User;
import cn.com.headfree.mouldshow.dao.TYearEffectInfoDao;
import cn.com.headfree.vo.simple.YearEffectInfoVo;
import com.gta.edu.sdk.base.dao.impl.BaseDaoImpl;
import com.gta.edu.sdk.common.dto.Page;
import com.gta.edu.sdk.util.StringUtil;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("/tYearEffectInfoDao")
public class TYearEffectInfoDaoImpl extends BaseDaoImpl<TYearEffectInfo> implements TYearEffectInfoDao {


    @Override
    public List<YearEffectInfoVo> findByBaseorgId(String deptId) throws Exception{
        Object[] params = new Object[1];
        StringBuilder sql = new StringBuilder("select t.id,t.erp_no,t.rate_year_start,t.rate_year_end,t.rate_year,t.perfor_grade,t.resi_inte,t.cur_inte,u.`NAME` as user_name from t_year_effect_info t INNER JOIN t_dept_emp_rela d on t.erp_no=d.erp_no INNER JOIN (select * from t_org_dept o where o.path like ?) o2 on o2.id=d.dept_id LEFT JOIN isc_user u on t.erp_no=u.FAX WHERE t.is_valid='0' AND t.del_sign='0' ORDER BY t.rate_year DESC");
//        StringBuilder sql = new StringBuilder("select t.id,t.erp_no,t.rate_year_start,t.rate_year_end,t.rate_year,GET_DIC('PerformLevelRecentThreeYears',t.perfor_grade) as perfor_grade,t.resi_inte,t.cur_inte,u.`NAME` as user_name from t_year_effect_info t INNER JOIN t_dept_emp_rela d on t.erp_no=d.erp_no INNER JOIN (select * from t_org_dept o where o.path like ?) o2 on o2.id=d.dept_id LEFT JOIN isc_user u on t.erp_no=u.FAX WHERE t.is_valid='0' AND t.del_sign='0' ORDER BY t.rate_year DESC");
//        StringBuilder sql = new StringBuilder("select t.id,t.erp_no,t.rate_year_start,DATE_FORMAT(t.rate_year_start,'%Y-%m-%d %H:%i:%s') as rate_year_start_str,t.rate_year_end,DATE_FORMAT(t.rate_year_end,'%Y-%m-%d %H:%i:%s') as rate_year_end_str,t.rate_year,GET_DIC('PerformLevelRecentThreeYears',t.perfor_grade) as perfor_grade,t.resi_inte,t.cur_inte,u.xb_name as user_name from t_year_effect_info t INNER JOIN t_dept_emp_rela d on t.erp_no=d.erp_no INNER JOIN (select * from t_org_dept o where o.path like ?) o2 on o2.id=d.dept_id INNER JOIN t_isc_user_tmp u on t.erp_no=u.hr_erp WHERE t.is_valid='0' AND t.del_sign='0' ORDER BY t.rate_year DESC");
        params[0] = "%"+deptId+"%";
        return this.findAllBySQL(YearEffectInfoVo.class, sql.toString(), null, false, params);
    }

    @Override
    public List<TYearEffectInfo> getTYearEffectInfoByErpNo(String erpNo, Integer year) {
        ArrayList<Object> arrayList = new ArrayList<Object>();
        String sql = "select t.* from t_year_effect_info t where t.is_valid=0 AND t.del_sign=0 " ;
        StringBuilder sqlBuilder = new StringBuilder(sql);
        sqlBuilder.append(" AND t.erp_no= ? ");
        arrayList.add(erpNo);
        sqlBuilder.append(" AND t.rate_year= ? ");
        arrayList.add(year);
        sqlBuilder.append(" order by t.rate_year DESC, create_time desc ");
        return this.findAllBySQL(TYearEffectInfo.class, sqlBuilder.toString(),null,false,arrayList.toArray());
    }

    @Override
    public void updateTYearEffectInfoByCondition(String erpNo, int year, String perforGrade){
        String sql = "update t_year_effect_info t set ";
        StringBuilder sqlBuilder = new StringBuilder(sql);
        sqlBuilder.append(" t.perfor_grade = #{perfor_grade,jdbcType=VARCHAR} ");
        sqlBuilder.append(" where t.is_valid='0' and t.del_sign = '0'  ");
        Map<String, Object> params = new HashMap<String, Object>();
        sqlBuilder.append(" and t.erp_no=#{erp_no,jdbcType=VARCHAR}");
        sqlBuilder.append(" and t.rate_year=#{rate_year,jdbcType=INTEGER} ");
        params.put("perfor_grade",perforGrade);
        params.put("erp_no",erpNo);
        params.put("rate_year",year);
        this.executeSql(sqlBuilder.toString(), params);
    }

    @Override
    public List<TSalaryInfo> getSalaryInfoList(String erpNo, Integer year) {
        ArrayList<Object> arrayList = new ArrayList<Object>();
        String sql = "select t.*,o.is_over from t_salary_info t " ;
        StringBuilder sqlBuilder = new StringBuilder(sql);
        sqlBuilder.append(" LEFT JOIN t_point_set_over o on t.year = o.year and t.org_id = o.org_id ");
        sqlBuilder.append(" where t.del_sign = 0 ");
        sqlBuilder.append(" and t.erp_no=? ");
        arrayList.add(erpNo);
        sqlBuilder.append(" AND t.year= ? ");
        arrayList.add(year);
        sqlBuilder.append(" order by t.create_time desc ");
        return this.findAllBySQL(TSalaryInfo.class, sqlBuilder.toString(),null,false,arrayList.toArray());
    }
}
